package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exection.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {
    @Autowired//房间service
    private RoomInfoService roomInfoService;
    @Autowired//公寓基本信息mapper
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired//图片的mapper
    private GraphInfoMapper graphInfoMapper;
    @Autowired//标签的mapper
    private LabelInfoMapper labelInfoMapper;
    @Autowired//配套的mapper
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired//杂费mapper
    private FeeValueMapper feeValueMapper;
    @Autowired//图片
    private GraphInfoService graphInfoService;
    @Autowired//标签和公寓中间表
    private ApartmentLabelService apartmentLabelService;
    @Autowired//杂费和公寓中间表
    private ApartmentFeeValueService apartmentFeeValueService;
    @Autowired//配套和公寓中间表
    private ApartmentFacilityService apartmentFacilityService;
    /**
     * 保存或更行公寓信息
     * 思路：ApartmentSubmitVo是一个中间表，先将单表数据插入
     * 如果id为空，则为插入操作，直接直接插入相应的数据
     * 如果id不为空，则为更新操作，先执行列表删除，再重新插入数据
     * */
    @Override
    public void saveOrUpdateApartment(ApartmentSubmitVo apartmentSubmitVo) {
//        判断传过来的对象参数id是否为空，为空则为添加操作，否则为更行操作
        boolean isUpdate = apartmentSubmitVo.getId() != null;
//        保存或更新公寓信息
        super.saveOrUpdate(apartmentSubmitVo);
        if (isUpdate){
            //1、删除图片列表
            LambdaQueryWrapper<GraphInfo> graphQueryWrapper = new LambdaQueryWrapper<>();
            graphQueryWrapper.eq(GraphInfo::getId,apartmentSubmitVo.getId());
            graphQueryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
            graphInfoService.remove(graphQueryWrapper);
            //2、删除标签列表
            LambdaQueryWrapper<ApartmentLabel> labelQueryWrapper = new LambdaQueryWrapper<>();
            labelQueryWrapper.eq(ApartmentLabel::getId,apartmentSubmitVo.getId());
            apartmentLabelService.remove(labelQueryWrapper);
            //3、删除杂费列表
            LambdaQueryWrapper<ApartmentFeeValue> feeQueryWrapper = new LambdaQueryWrapper<>();
            feeQueryWrapper.eq(ApartmentFeeValue::getId,apartmentSubmitVo.getId());
            apartmentFeeValueService.remove(feeQueryWrapper);
            //4、删除配套列表
            LambdaQueryWrapper<ApartmentFacility> facilityQueryWrapper = new LambdaQueryWrapper<>();
            facilityQueryWrapper.eq(ApartmentFacility::getId,apartmentSubmitVo.getId());
            apartmentFacilityService.remove(facilityQueryWrapper);
        }
        //1、插入图片列表
//        获取参数里发送到图片信息集合
        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
//        判断参数里发送到图片信息集合是否为空
        if (!CollectionUtils.isEmpty(graphVoList)){
//            创建一个泛型为GraphInfo的集合数组，类似于工具数组
            ArrayList<GraphInfo> graphInfoList = new ArrayList<>();
            for (GraphVo graphVo : graphVoList) {//遍历从参数里获取的图片信息集合
//                创建一个graphinfo对象，用于接收存储提取出来的参数图片信息集合
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setId(apartmentSubmitVo.getId());//从参数里提取图片id值
                graphInfo.setItemType(ItemType.APARTMENT);//从枚举类ItemType里获取公寓类型
                graphInfo.setName(graphVo.getName());//从参数值提取出来的图片信息集合中提取出name和url
                graphInfo.setUrl(graphVo.getUrl());
                graphInfoList.add(graphInfo);//将经过提取属性赋值后的对象添加到graphInfo工具数组中
            }
            graphInfoService.saveBatch(graphInfoList);//批量保存到数据库中
        }
        //2、插入标签列表
        List<Long> labelIdList = apartmentSubmitVo.getLabelIds();
        if (!CollectionUtils.isEmpty(labelIdList)){
            ArrayList<ApartmentLabel> apartmentLabels = new ArrayList<>();
            for (Long labelVoId : labelIdList) {
                ApartmentLabel build = ApartmentLabel
                        .builder()
                        .apartmentId(apartmentSubmitVo.getId())
                        .labelId(labelVoId).build();
                apartmentLabels.add(build);
            }
            apartmentLabelService.saveBatch(apartmentLabels);
        }
        //3、插入杂费列表
        List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
        if(!CollectionUtils.isEmpty(feeValueIds)){
            ArrayList<ApartmentFeeValue> apartmentFeeValues = new ArrayList<>();
            for (Long feeValueId : feeValueIds) {
                ApartmentFeeValue build = ApartmentFeeValue.builder().apartmentId(apartmentSubmitVo.getId()).feeValueId(feeValueId).build();
                apartmentFeeValues.add(build);
            }
            apartmentFeeValueService.saveBatch(apartmentFeeValues);
        }
        //4、插入配套列表
        List<Long> facilityInfoIds = apartmentSubmitVo.getFacilityInfoIds();
        if (!CollectionUtils.isEmpty(facilityInfoIds)){
            ArrayList<ApartmentFacility> apartmentFacilities = new ArrayList<>();
            for (Long facilityInfoId : facilityInfoIds) {
                ApartmentFacility build = ApartmentFacility.builder().apartmentId(apartmentSubmitVo.getId()).facilityId(facilityInfoId).build();
                apartmentFacilities.add(build);
            }
            apartmentFacilityService.saveBatch(apartmentFacilities);
        }
    }

    /**
     * 根据条件分页查询公寓列表
     *
     * @param page:分页对象
     * @param apartmentQueryVo：条件查询的对象
     */
    @Override
    public IPage<ApartmentItemVo> iPageResult(Page<ApartmentItemVo> page, ApartmentQueryVo apartmentQueryVo) {
        return apartmentInfoMapper.iPageResult(page,apartmentQueryVo);
    }

    /**
     * 根据id获取公寓详细信息
     *
     * @param id
     */
    @Override
    public ApartmentDetailVo getApartmentDetail(Long id) {
//        1.根据id查询公寓表全部信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(id);
//        2.根据查询的公寓全部信息查询 图片列表
        List<GraphVo> graphVoList = graphInfoMapper.selectByApartmentId(ItemType.APARTMENT, id);
//        3.根据查询的公寓全部信息查询 标签列表
        List<LabelInfo> labelInfoList = labelInfoMapper.selectByApartmentId(id);
//        4.根据查询的公寓全部信息查询 配套列表
        List<FacilityInfo> facilityInfoList= facilityInfoMapper.selectByApartmentId(id);
//        5.根据查询的公寓全部信息查询 杂费列表
        List<FeeValueVo> feeValueVoList =feeValueMapper.selectByApartmentId(id);
//        6.拼接查询信息
        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
//    自动赋值 ，类似于set方法   将1，中查询的信息赋值为apartmentDetailVo对象中
        BeanUtils.copyProperties(apartmentInfo, apartmentDetailVo);
        apartmentDetailVo.setGraphVoList(graphVoList);
        apartmentDetailVo.setLabelInfoList(labelInfoList);
        apartmentDetailVo.setFacilityInfoList(facilityInfoList);
        apartmentDetailVo.setFeeValueVoList(feeValueVoList);
        return apartmentDetailVo;
    }

    /**
     * 根据id删除公寓信息
     *
     * @param id
     */
    @Override
    public void removeApartmentById(Long id) {
        long count = roomInfoService.count(new LambdaQueryWrapper<RoomInfo>().eq(RoomInfo::getApartmentId, id));
        if (count>0){
            //该公寓下有房间，不能删除
            throw new LeaseException(ResultCodeEnum.ADMIN_ACCESS_APARTMENT_DELETE);
        }
        //1.删除公寓基本信息
        apartmentInfoMapper.deleteById(id);
        //2.删除配套基本信息
        apartmentFacilityService.remove(new LambdaQueryWrapper<ApartmentFacility>().eq(ApartmentFacility::getApartmentId,id));
        //3.删除标签基本信息
        apartmentLabelService.remove(new LambdaQueryWrapper<ApartmentLabel>().eq(ApartmentLabel::getApartmentId,id));
        //4.删除杂费值基本信息
        apartmentFeeValueService.remove(new LambdaQueryWrapper<ApartmentFeeValue>().eq(ApartmentFeeValue::getApartmentId,id));
        //5.删除图片基本信息
        graphInfoService.remove(new LambdaQueryWrapper<GraphInfo>().eq(GraphInfo::getItemId,id).eq(GraphInfo::getItemType,ItemType.APARTMENT));
    }

}




